/*
 * Copyright (C) 2012 mAPPn.Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mol.market.common.network;

import android.content.Context;
import android.text.TextUtils;

import com.mol.market.Session;

/**
 * API请求参数代理。
 * <p/>
 *
 * API请求会使用一些公有的参数，比如OS版本，MAC地址等等。此类帮助设置这样的参数。
 *
 * Author: Andrew.Wang
 * Date  : 12-9-25
 */
public class ApiRequestProxy {

  /**
   * 未知用户ID
   */
  private static final String UNKNOWN_USER = "-1";

  /**
   * 对请求参数进行代理配置
   * @param ctx     Application Context
   * @param request API请求对象
   */
  public static void doProxy(Context ctx, ApiRequest request) {

    Session session = Session.get(ctx);
    int actionId = request.actionId;

    if (ApiRequestFactory.UCENTER_API.contains(actionId)) {
      request.requestHeader.putString("Accept-Encoding", "identity");
      request.requestHeader.putString("User-Agent", session.getUCenterApiUserAgent());
    } else if (ApiRequestFactory.S_XML_REQUESTS.contains(actionId)) {
      request.requestHeader.putString("Accept-Encoding", "gzip");
      request.requestHeader.putString("G-Header", session.getJavaApiUserAgent());
    }

    // 需要使用「MAC」和「IMEI」
    if (actionId == MarketAPI.ACTION_GET_EXCHANGE_DETAIL
        || actionId == MarketAPI.ACTION_GET_EXCHANGE_REWARD) {

      request.args.putString("imei", session.getIMEI());
      request.args.putString("mac", session.getMac());
    }

    // 需要使用「UID」
    if (actionId == MarketAPI.ACTION_IFTTT_REPORT
        || actionId == MarketAPI.ACTION_MARK_READ
        || actionId == MarketAPI.ACTION_GET_FOLLOWED_RECOMMEND
        || actionId == MarketAPI.ACTION_FOLLOW_MASTER
        || actionId == MarketAPI.ACTION_BIND_ACCOUNT
        || actionId == MarketAPI.ACTION_ADD_COMMENT
        || actionId == MarketAPI.ACTION_ADD_RATING
        || actionId == MarketAPI.ACTION_GET_DOWNLOAD_URL
        || actionId == MarketAPI.ACTION_GET_CONSUMESUM
        || actionId == MarketAPI.ACTION_SYNC_BUYLOG
        || actionId == MarketAPI.ACTION_GET_MYRATING
        || actionId == MarketAPI.ACTION_GET_CONSUME_DETAIL
        || actionId == MarketAPI.ACTION_GET_BALANCE
        || actionId == MarketAPI.ACTION_GET_PAY_LOG
        || actionId == MarketAPI.ACTION_GET_ALIPAY_ORDER_INFO
        || actionId == MarketAPI.ACTION_ADD_DISCUSS
        || actionId == MarketAPI.ACTION_DOWN_REPORT) {

      request.args.putString("uid",
          TextUtils.isEmpty(session.getUid()) ? UNKNOWN_USER : session.getUid());
    }

    // 需要「系统版本」，「屏幕尺寸」，「是否过滤应用」
    if (actionId == MarketAPI.ACTION_GET_MESSAGE
        || actionId == MarketAPI.ACTION_MARK_READ
        || actionId == MarketAPI.ACTION_GET_HOME_MASTER_RECOMMEND
        || actionId == MarketAPI.ACTION_GET_FOLLOWED_RECOMMEND
        || actionId == MarketAPI.ACTION_GET_REQUIRED
        || actionId == MarketAPI.ACTION_GET_RECOMMEND_NOTIFICATION
        || actionId == MarketAPI.ACTION_GET_TOP_RECOMMEND
        || actionId == MarketAPI.ACTION_GET_ALL_CATEGORY
        || actionId == MarketAPI.ACTION_GET_HOME_RECOMMEND
        || actionId == MarketAPI.ACTION_GET_RANK_BY_CATEGORY
        || actionId == MarketAPI.ACTION_GET_GROW_FAST
        || actionId == MarketAPI.ACTION_PAID_RANK
        || actionId == MarketAPI.ACTION_GET_RECOMMEND_PRODUCTS
        || actionId == MarketAPI.ACTION_GET_CATEGORY
        || actionId == MarketAPI.ACTION_SEARCH
        || actionId == MarketAPI.ACTION_GET_TOPIC
        || actionId == MarketAPI.ACTION_GET_MASTER_RECOMMEND
        || actionId == MarketAPI.ACTION_GET_MASTER_RECOMMEND_APPS
        || actionId == MarketAPI.ACTION_GET_MASTER_CONTAINS
        || actionId == MarketAPI.ACTION_GET_RECOMMEND_BY_APP
        || actionId == MarketAPI.ACTION_GET_CATEGORY_TAGS
        || actionId == MarketAPI.ACTION_GET_TAGS_DETAILS
        || actionId == MarketAPI.ACTION_GET_TAGS_IN_DETAILS) {

      request.args.putString("platform", String.valueOf(session.getOsVersion()));
      request.args.putString("screen_size", session.getScreenSize());
      request.args.putString("match_type", String.valueOf(session.isFilterApps()));
    }

  }
}
